package com.asyf.provider.controller;

import com.asyf.provider.service.TccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * TCC与AT模式相同，也是二阶段提交，但是TCC对业务代码侵入性很强
 * TCC模式下，所有事务都要手动实现Try，Confirm，Cancel三个方法
 */
@Api(tags = "seata事务TCC模式")
@RestController
@RequestMapping(value = "/tcc")
public class TccController {

    @Autowired
    private TccService tccService;


    @ApiOperation(value = "单机事务测试")
    @RequestMapping(value = "/singleTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public String singleTest(int a) {
        return tccService.singleTest(a);
    }

    //注意：测试的事务不要使用xa的数据源
    @ApiOperation(value = "事务测试(oracle+mysql)")
    @RequestMapping(value = "/demo2", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public String demo2(int a) {
        return tccService.demo2(a);
    }

}
/**
 * 笔记
 * 1注解：@BusinessActionContextParameter
 * 注解标记的参数会在上下文中传播，即能通过BusinessActionContext对象在commit方法及cancel方法中取到该参数值
 */